package web;

import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class UserMainServlet extends HttpServlet {
    // 页面判断接口

    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }


    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        String userNum = "";
        Cookie[] cookies = req.getCookies();
        for(Cookie cookie:cookies) {
            System.out.println(cookie.getName() + " " + cookie.getValue());
            if("userNum".equals(cookie.getName())) {
                userNum = cookie.getValue();
            }
        }
        if(userNum=="") {
            // 404
            resp.sendRedirect("/webdemo2/views/error.html");
        } else {
            System.out.println(userNum + " 准许登录user.html ");
            resp.sendRedirect("/webdemo2/views/userspace.html");
        }
    }
}
